<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>登录</title>
  <!-- <link rel="stylesheet" type="text/css" href="css/main.css"> -->
  <link rel="stylesheet" type="text/css" href="../res/static/css/main.css">
  <link rel="stylesheet" href="https://www.layuicdn.com/layui/css/layui.css" media="all">
  <script src="https://www.layuicdn.com/layui/layui.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.js"></script>
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
</head>
<style>

</style>

<body>
<div class="content content-nav-base  login-content">
  <div class="site-nav-bg">
    <div class="site-nav w1200">
      <p class="sn-back-home">
        <i class="layui-icon layui-icon-home"></i>
        <a href="#">首页</a>
      </p>
    </div>
  </div>

  <div class="login-bg" id="app">
    <div class="login-cont w1200">
      <div class="form-box">
        <ul class="layui-tab-title">
          <li class="layui-this" onclick="yincangZ()">登录</li>
          <li onclick="yincangD()" id="lzhuce">注册</li>
        </ul>
        <form class="layui-form" action="" id="formD">
          <legend>学号登录</legend>
          <div class="layui-form-item">
            <div class="layui-inline veri-code">
              <div class="layui-input-inline">
                <input type="text" name="uname" id="uname" lay-verify="required" placeholder="请输入学号"
                       autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline veri-code">
              <div class="layui-input-inline">
                <input id="pwd" type="password" name="pwd" lay-verify="required" placeholder="请输入密码"
                       autocomplete="off" class="layui-input">

              </div>
            </div>
          </div>
          <div class="layui-form-item login-btn">
            <div class="layui-input-block">
              <button class="layui-btn" lay-submit="" lay-filter="demo1" id="find" onclick="return false">登录</button>
            </div>
          </div>
        </form>

        <form class="layui-form" action="" id="formZ" hidden>
          <legend>学号注册</legend>
          <div class="layui-form-item">
            <div class="layui-inline veri-code">
              <div class="layui-input-inline">
                <input type="text" name="uname" id="unameZ" lay-verify="required" placeholder="请输入学号"
                       autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline veri-code">
              <div class="layui-input-inline">
                <input id="pwdZ" type="password" name="pwd" lay-verify="required" placeholder="请输入密码"
                       autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline veri-code">
              <div class="layui-input-inline">
                <input id="pwdZ1" type="password" name="pwd" lay-verify="required" placeholder="请确认密码"
                       autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>
          <div class="layui-form-item login-btn">
            <div class="layui-input-block">
              <button class="layui-btn" lay-submit="" lay-filter="demo1" id="register"
                      onclick="return false">注册</button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
</body>
<script type="text/javascript">
  //获取cookie
  function setCookie(name, value) {
    if (!name || !value) return;
    var Days = 30;//默认30天
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + encodeURIComponent(value) + ";expires=" + exp.toUTCString();
  }

  layui.use(['jquery', 'form', 'layer', 'element'], function () {
    var $ = layui.$, form = layui.form;
    var layer = layui.layer;
    var element = layui.element;

    //登录
    $("#find").click(function () {
      if (!/^1\d{2}$/.test($("#uname").val())) {
        layer.msg("请输入正确的学号");
        return false;
      }
      $.ajax({
        type: "get",
        url: "login.action",
        data: {
          'stuno': $("#uname").val(),
          'password': $("#pwd").val()
        },
        dataType: "json",//返回的
        success: function (data) {
          if (data.msg == 4) {
            layer.open({
              content: '登录成功！'
              , btn: ['确定']
              , yes: function (index, layero) {
                //按钮【按钮一】的回调
                var stuno = $("#uname").val();
                var url = "welcome.html?stuno=" + stuno;
                window.location.href = url;

              },
              cancel: function () {
                //右上角关闭回调

                //return false 开启该代码可禁止点击该按钮关闭
              }
            });
          } else if (data.msg == 0) {
            layer.alert('学号不存在', { title: '登录' }, function (index) {
              layer.close(index);
              location.href = "login.html";
            });
          } else if (data.msg == 1) {
            layer.alert('学号未注册', { title: '登录' }, function (index) {
              layer.close(index);
              location.href = "login.html";
            });
          } else if (data.msg == 2) {
            layer.alert('学号注册未通过', { title: '登录' }, function (index) {
              layer.close(index);
              location.href = "login.html";
            });
          } else if (data.msg == 3) {
            layer.alert('密码错误', { title: '登录' }, function (index) {
              layer.close(index);
              location.href = "login.html";
            });
          }
        },
        error: function (msg) {
          console.log(msg);
          layer.alert('登录失败', { title: '登录' }, function (index) {
            layer.close(index);
            location.href = "login.html";
          });
        }
      });

    })
    //注册
    $("#register").click(function () {
      if (!/^1\d{2}$/.test($("#unameZ").val())) {
        layer.msg("请输入正确的学号");
        return false;
      }
      else {
        var msg = $("#pwdZ").val();
        if (msg == $("#pwdZ1").val()) {
          $.ajax({
            type: "get",
            url: "registerStudent.action",
            data: {
              'stuno': $("#unameZ").val(),
              'password': $("#pwdZ").val()
            },
            dataType: "json",//返回的
            success: function (data) {
              if (data.result == 3) {
                layer.alert('注册成功', { title: '注册' }, function (index) {
                  layer.close(index);
                  location.href = "index.html";
                });
              } else if (data.msg == 0) {
                layer.alert('学号不存在', { title: '注册' }, function (index) {
                  layer.close(index);
                  location.href = "login.html";
                });
              } else if (data.msg == 1) {
                layer.alert('已提交注册申请', { title: '注册' }, function (index) {
                  layer.close(index);
                  location.href = "login.html";
                });
              } else if (data.msg == 2) {
                layer.alert('已经注册', { title: '注册' }, function (index) {
                  layer.close(index);
                  location.href = "login.html";
                });
              }
            },
            error: function (msg) {
              console.log(msg);
              layer.alert('注册失败', { title: '注册' }, function (index) {
                layer.close(index);
                location.href = "login.html";
              });
            }
          });
        }
        else {
          // alert("密码不一致！");
          layer.open({
            title: '注册'
            , content: '两次密码不一致！'
          });
        }
        console.log(msg);

      }
    })
  })
  //显示登录页
  function yincangZ() {
    layui.use(['jquery'], function () {
      var $ = layui.$;
      console.log("登录");
      $("#formD").show();
      $("#formZ").hide();
    });
  }
  //显示注册页
  function yincangD() {
    layui.use(['jquery'], function () {
      var $ = layui.$;
      console.log("注册");
      $("#formD").hide();
      $("#formZ").show();
    });
  }
</script>


</html>